//
//  ViewController.m
//  TestAudio
//
//  Created by 悠哉小哥哥 on 2018/7/2.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "ViewController.h"

#import "AudioCell.h"


#define CELL_ID @"ViewController.h"


@interface ViewController ()<UITableViewDelegate, UITableViewDataSource, AVAudioPlayerDelegate, AudioCellDelegate>

@property (nonatomic, strong) AVAudioPlayer *audioPlayer;

@property (nonatomic, strong) NSArray *arr;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, assign) NSInteger cellIndex;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
    self.cellIndex = -1;
    
    
    [self UI];
    
    
    
    CGFloat sec = [self getMusicTime:[NSURL URLWithString:@"https://www.mynew-miracle.com/Public/Uploads/2017071608385921103.speex.mp3"]];
    
    NSLog(@"sec = %lf", sec);
 
    
    
    self.arr = @[@"https://www.mynew-miracle.com/Public/Uploads/2017071608385921103.speex.mp3", @"https://www.mynew-miracle.com/Public/Uploads/201707230939227143.speex.mp3", @"https://www.mynew-miracle.com/Public/Uploads/2017073008220721162.speex.mp3", @"https://www.mynew-miracle.com/Public/Uploads/201708191941587641.speex.mp3"];
    
    

    
    [self.tableView reloadData];
    
}


- (CGFloat)getMusicTime:(NSURL *)url{
    
    AVURLAsset *avURLAsset = [[AVURLAsset alloc] initWithURL:url options:nil];
    
    // 获取音频总时长
    
    CGFloat time = avURLAsset.duration.value / avURLAsset.duration.timescale;
    
    return time;
    
}



//
- (void)UI{
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    
    tableView.delegate = self;
    tableView.dataSource = self;
    
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.view addSubview:tableView];
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.edges.equalTo(self.view);
    }];
    
    
    tableView.rowHeight = UITableViewAutomaticDimension;
    tableView.estimatedRowHeight = 60;
    
    
    [tableView registerClass:[AudioCell class] forCellReuseIdentifier:CELL_ID];
 
    self.tableView = tableView;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    AudioCell *cell = [tableView dequeueReusableCellWithIdentifier:CELL_ID];
    
    cell.audioDelegate = self;
    cell.stringAudio = self.arr[indexPath.row];
    
    
    if (self.cellIndex == indexPath.row) {
        
        cell.boolChecked = YES;
        cell.slider.userInteractionEnabled = YES;
        
    }else{
        
        cell.boolChecked = NO;
        cell.slider.userInteractionEnabled = NO;
    }
    
    
    __weak typeof(self) weakSelf = self;
    cell.indexBlock = ^(NSIndexPath *indexP) {
        
        weakSelf.cellIndex = indexP.row;
        [weakSelf.tableView reloadData];
    };
    
    
    return cell;
}


- (NSIndexPath *)audioCellClickControlCell:(AudioCell *)audioCell{
    
    NSIndexPath *indexPath = [self.tableView indexPathForCell:audioCell];
    
    return indexPath;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
